SQL (Structured Query Language) Best Practices

SQL Format

  1. Capitalize all letters in SQL keywords such as SELECT,JOIN or WHERE.
     

  2. String (text) variables in SQL statements should have brackets around them and then single quotes around them.  
     

  3. Integer variables in SQL statements should have brackets around them without quotes.
     

  4. Avoid using SQL inside Math Loops.

  5. Consider whether the query could return one or more than one result and configure it appropriately.  

  6. COLLECTION SQL should return two elements: Name and Value with multiple rows.  When SQL is used to fill a field’s combo box, the SQL should return two columns and multiple rows.
     

  7. In a COLLECTION if the KEY is the same name as a field name, then concatenate a string to the first column returned in the COLLECTION SQL.

  8. SQL that is used to fill a field combo box should return two values.  

  9. When possible, subqueries should be avoided and JOINs should be used for better performance.

  10. Organize table names used in SQLs with the selection criteria of that table.  The FROM table will be last using WHERE, all other tables (through JOINs) should use AND.

  11. Only one table name should be after FROM.  The remaining tables will be part of the SQL through JOIN.

  12. When using ThisPendingActivityXML, use the following configuration to help performance:

  1. Use ThisPendingActivityXML as needed then do:

 

Retrieving Data

  1. Use SQL to retrieve data from the database.

  2. Don’t JOIN tables unnecessarily.  

  3. When performing SQL to select one or more AsActivity records, be sure to consider AsActivity.StatusCode and AsActivity.TypeCode values to include/exclude records as desired.

  4. When querying for the most recent transaction of a certain type, query by EffectiveDate DESC, ProcessingOrder DESC, ActivityGMT DESC and then fetch first row only.  (Also see #1 above).
     

  5. If a piece of information is already defined as a field, don’t use a SQL to retrieve this information again from the database.  Since this value is already a <Field>, change to TYPE=”FIELD” and reference the field name.
     

  6. Use PLANFIELD, POLICYFIELD, or SEGMENTFIELD when retrieving information from AsPlanField, policy information, or a segment.

Example XML

<MathVariable VARIABLENAME="SegmentGUIDArray" TYPE="NUMERICARRAY" OPERATION="FILLBY-SQL">Write SQL to retrieve the SegmentGUID that you would like to retrieve a Segment Field</MathVariable>

<MathVariable VARIABLENAME="SegmentGUIDArray_index" TYPE="VALUE">0</MathVariable>

<MathVariable VARIABLENAME="SegmentFieldRetrieved" TYPE="SEGMENTFIELD" SOURCEARRAY="SegmentGUIDArray">Name of the Field you would like to retrieve</MathVariable>
 

  1. In fields, when defining a combo box, use SQL queries calling ASCODE rather than redefining as fixed queries in business rules when possible.  This will help with standardization and maintenance.

  2. Use SQL as little as possible.  Use COLLECTION to bring back as much usable data as possible in one round trip to the database.  Use CollectionValue to parse the individual pieces of data from the COLLECTION.